﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class View_ManageBusRides : System.Web.UI.Page
{

    private ManageBus mnBus;
    private string SESSION_SEARCH = "SEARCH";
    protected void Page_Load(object sender, EventArgs e)
    {
        mnBus = new ManageBus();
        if (!IsPostBack)
        {
            Session.Remove(SESSION_SEARCH);
        }
        ScriptManager.GetCurrent(Page).RegisterPostBackControl(tbnExport);
    }

    /// <summary>
    /// Su kien click tren button search
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string stringSearch = txtKeyWord.Text.Trim();
        if (txtKeyWord.Text.Trim() == String.Empty)
        {
            stringSearch = hdfSearchString.Value.Trim();
        }

        if (Session[SESSION_SEARCH] != null)
        {
            if (hdfSearchString.Value.Trim() != Session[SESSION_SEARCH].ToString())
            {
                stringSearch = hdfSearchString.Value.Trim();
            }
            if (Session[SESSION_SEARCH].ToString() != txtKeyWord.Text.Trim())
            {
                stringSearch = txtKeyWord.Text.Trim();
            }
        }

        hdfSearchString.Value = stringSearch;
        rdgBus.DataSource = mnBus.searchBusByKeyword(stringSearch);
        rdgBus.Rebind();
        Session[SESSION_SEARCH] = stringSearch;
        txtKeyWord.Text = stringSearch;
    }

    /// <summary>
    /// Ham day du lieu vao radGrid
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rdGDriver_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        if ((Session[SESSION_SEARCH] != null) && (Session[SESSION_SEARCH].ToString() != string.Empty))
        {
            rdgBus.DataSource = mnBus.searchBusByKeyword(Session[SESSION_SEARCH].ToString());
        }
        else
        {
            rdgBus.DataSource = mnBus.getAllBus();
        }
    }

    /// <summary>
    /// Ham insert du lieu vao radGrid
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rdGDriver_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        Bus bus = new Bus();
        TextBox txtRegistrationNumber = editedItem.FindControl("txtRegistrationNumber") as TextBox;
        TextBox txtNumberMachine = editedItem.FindControl("txtNumberMachine") as TextBox;
        TextBox txtNumberChassis = editedItem.FindControl("txtNumberChassis") as TextBox;
        TextBox txtCapacity = editedItem.FindControl("txtCapacity") as TextBox;
        TextBox txtStatusBus = editedItem.FindControl("txtStatusBus") as TextBox;
        RadDatePicker rdpStartDateUse = editedItem.FindControl("rdpStartDateUse") as RadDatePicker;
        TextBox txtNote = editedItem.FindControl("txtNote") as TextBox;
        bus.RegistrationNumber = txtRegistrationNumber.Text;
        bus.NumberChassis = txtNumberChassis.Text;
        bus.NumberMachine = txtNumberMachine.Text;
        bus.StatusBus = txtStatusBus.Text;
        bus.Note = txtNote.Text;

        DateTime temp;
        DateTime.TryParse(rdpStartDateUse.SelectedDate.ToString(), out temp);
        bus.StartDateUse = temp;
        int Capacity;
        bus.Capacity = int.TryParse(txtCapacity.Text, out Capacity) ? Capacity : 0;

        int _result = mnBus.insertNewBus(bus);
        if (_result == 1)
            Message.Show(rapBusRides, "Thêm dữ liệu thành công!", Message.Type.Success);
        else if (_result == 0)
            Message.Show(rapBusRides, "Thêm dữ liệu không thành công: đã tồn tại xe", Message.Type.Error);
        else
            Message.Show(rapBusRides, "Thêm không thành công : Lỗi hệ thống", Message.Type.Error);
        rdgBus.CurrentPageIndex = 0;
    }

    /// <summary>
    /// Ham delete 1 hang cua radgrid
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rdGDriver_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        int pageIndex = rdgBus.CurrentPageIndex;
        GridEditableItem editedItem = e.Item as GridEditableItem;
        Bus bus = new Bus();
        int IDBus;
        bus.IDBus = int.TryParse(editedItem.OwnerTableView.DataKeyValues[e.Item.ItemIndex][BusColumn.IDBus].ToString(), out IDBus) ? IDBus : 0;
        int _result = mnBus.deleteBus(bus);
        if (_result == 1)
            Message.Show(rapBusRides, "Xóa dữ liệu thành công!", Message.Type.Success);
        else if (_result == 0)
            Message.Show(rapBusRides, "Xóa dữ liệu không thành công1", Message.Type.Error);
        else
            Message.Show(rapBusRides, "Xóa không thành công : Lỗi hệ thống", Message.Type.Error);
        rdgBus.CurrentPageIndex = pageIndex;
    }

    /// <summary>
    /// Kiem tra check luc day du lieu vao radGrid
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rdGDriver_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            Label lblDateOfBirth = (Label)item.FindControl("lblStartDateUser");
            DateTime temp;
            DateTime.TryParse(lblDateOfBirth.Text, out temp);
            lblDateOfBirth.Text = String.Format("{0:dd/MM/yyyy}", temp);

            ImageButton Edit = ((ImageButton)item["Edit"].Controls[0]);
            Edit.CssClass = "btnEditDelete";
            Edit.Width = 20;
            Edit.Height = 20;
            Edit.Attributes.Add("style", "background-color:transparent");
            ImageButton Delete = ((ImageButton)item["Delete"].Controls[0]);
            Delete.CssClass = "btnEditDelete";
            Delete.Width = 20;
            Delete.Height = 20;
            Delete.Attributes.Add("style", "background-color:transparent;");
        }
        if (e.Item.IsInEditMode)
        {
            GridEditFormItem item = e.Item as GridEditFormItem;
            Button btnAdd = new Button();
            btnAdd = item.FindControl("PerformInsertButton") as Button;
            if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
            {
                btnAdd.Text = "Thêm mới";
                btnAdd.CssClass = "myButton";
            }
            else
            {
                Button btnUpdate = new Button();
                btnUpdate = item.FindControl("UpdateButton") as Button;
                btnUpdate.Text = "Chỉnh sửa";
                btnUpdate.CssClass = "myButton";
            }
            Button btnCancel = new Button();
            btnCancel = item.FindControl("CancelButton") as Button;
            btnCancel.Text = "Hủy bỏ";
            btnCancel.CssClass = "myButton";
        }
    }

    /// <summary>
    /// ham update 1 item
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rdGDriver_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        int pageIndex = rdgBus.CurrentPageIndex;
        GridEditableItem editedItem = e.Item as GridEditableItem;
        String RNID = editedItem.OwnerTableView.DataKeyValues[e.Item.ItemIndex][BusColumn.IDBus].ToString();
        Bus bus = new Bus();
        int IDBus;
        bus.IDBus = int.TryParse(editedItem.OwnerTableView.DataKeyValues[e.Item.ItemIndex][BusColumn.IDBus].ToString(), out IDBus) ? IDBus : 0;
        TextBox txtRegistrationNumber = editedItem.FindControl("txtRegistrationNumber") as TextBox;
        TextBox txtNumberMachine = editedItem.FindControl("txtNumberMachine") as TextBox;
        TextBox txtNumberChassis = editedItem.FindControl("txtNumberChassis") as TextBox;
        TextBox txtCapacity = editedItem.FindControl("txtCapacity") as TextBox;
        TextBox txtStatusBus = editedItem.FindControl("txtStatusBus") as TextBox;
        RadDatePicker rdpStartDateUse = editedItem.FindControl("rdpStartDateUse") as RadDatePicker;
        TextBox txtNote = editedItem.FindControl("txtNote") as TextBox;
        bus.RegistrationNumber = txtRegistrationNumber.Text;
        bus.NumberChassis = txtNumberChassis.Text;
        bus.NumberMachine = txtNumberMachine.Text;
        bus.StatusBus = txtStatusBus.Text;
        bus.Note = txtNote.Text;

        DateTime temp;
        DateTime.TryParse(rdpStartDateUse.SelectedDate.ToString(), out temp);
        bus.StartDateUse = temp;
        int Capacity;
        bus.Capacity = int.TryParse(txtCapacity.Text, out Capacity) ? Capacity : 0;

        int _result = mnBus.updateBus(bus);
        if (_result == 1)
            Message.Show(rapBusRides, "Chỉnh sửa dữ liệu thành công!", Message.Type.Success);
        else if (_result == 0)
            Message.Show(rapBusRides, "Chỉnh sửa dữ liệu không thành công1", Message.Type.Error);
        else
            Message.Show(rapBusRides, "Chỉnh sửa dữ liệu không thành công : Lỗi hệ thống", Message.Type.Error);
        rdgBus.CurrentPageIndex = pageIndex;
    }
  
    protected void tbnExport_Click(object sender, EventArgs e)
    {
        rdgBus.ExportSettings.Excel.Format = (GridExcelExportFormat)Enum.Parse(typeof(GridExcelExportFormat), "ExcelML");
        rdgBus.ExportSettings.IgnorePaging = true;
        rdgBus.ExportSettings.ExportOnlyData = true;
        rdgBus.ExportSettings.OpenInNewWindow = true;
        rdgBus.ExportSettings.FileName = "Danh-sach-xe-" + DateTime.Now.ToString();
        rdgBus.MasterTableView.ExportToExcel();
    }
}